//
//  MeController.m
//  ai2ban
//
//  Created by mac book pro on 2018/1/6.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "MeController.h"
#import "MeInfoController.h"
#import "MeOrderController.h"
#import "LoginViewViewController.h"
#import "MeCollectViewController.h"
#import "ChangePsdViewController.h"
#import "NetworkManager.h"
#import "UserModel.h"
#import "MessageController.h"

@interface MeController ()

@property (weak, nonatomic) IBOutlet UIScrollView *bgScrollView;
@property (weak, nonatomic) IBOutlet UIButton *waiterPayView;
@property (weak, nonatomic) IBOutlet UIButton *twoView;
@property (weak, nonatomic) IBOutlet UIButton *threeView;
@property (weak, nonatomic) IBOutlet UIButton *fourView;
@property (weak, nonatomic) IBOutlet UILabel *userNameLab;
@property (weak, nonatomic) IBOutlet UILabel *userPhoneLab;
@property (weak, nonatomic) IBOutlet UIImageView *userHeaderImgV;
@property (weak, nonatomic) IBOutlet UIButton *JiFenNumberBtn;
@property (weak, nonatomic) IBOutlet UILabel *SavemoneyNumberLab;
@property (weak, nonatomic) IBOutlet UILabel *allPayScoreNumLab;



@end

@implementation MeController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadUserData) name:UpdateUserDataNotification object:nil];
    self.bgScrollView.contentInset = UIEdgeInsetsMake(0, 0, self.navigationController.navigationBar.size.height, 0);
    [self loadUserData];
    [self setUserData];
    [self setUpUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:true];
}

-(void)viewWillDisappear:(BOOL)animated {
    [self.navigationController.navigationBar setHidden:false];
}
- (void)setUpUI {
    self.userHeaderImgV.clipsToBounds = true;
    self.userHeaderImgV.layer.cornerRadius = 27.5;
    self.userHeaderImgV.layer.borderColor = [kWhiteColor CGColor];
    self.userHeaderImgV.layer.borderWidth = 2;
    self.JiFenNumberBtn.clipsToBounds = true;
    self.JiFenNumberBtn.layer.cornerRadius = 4;
}

- (void) setUserData {
    [self.userHeaderImgV sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", ImageBaseUrl,[AppManager user].picImg]] placeholderImage:[UIImage imageNamed:@"logo"]];
    self.userNameLab.text = [AppManager user].userName ? [AppManager user].userName : @"点击登录";
    self.userPhoneLab.hidden = ![AppManager user].telephone;
    self.userPhoneLab.text = [AppManager user].telephone;
    [self.JiFenNumberBtn setTitle:AppManager.scoreNumber forState:UIControlStateNormal];
    self.SavemoneyNumberLab.text = [NSString stringWithFormat:@"¥%@", AppManager.user.scoreModel[@"num"]];
    self.allPayScoreNumLab.text = [NSString stringWithFormat:@"%@笔", AppManager.user.scoreModel[@"num_score"]];
}


- (void) loadUserData {
    [ActivityIndicatorView show];
    [[NetworkManager shareNetwork] requestWithURLString:[NSString stringWithFormat:@"app/administrator/list/%@", [AppManager userId]] parameters:nil method:@"" callBack:^(id responseObject) {
        [ActivityIndicatorView dismiss];
        if ([responseObject[@"state"] intValue]) {
            AppManager.user = [UserModel mj_objectWithKeyValues:responseObject[@"data"]];
            [AppManager loadUserAddress];
            [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getScoreHead" parameters:@{@"userId": AppManager.userId} method:@"" callBack:^(id responseObject) {
                if ([responseObject[@"state"] intValue]) {
                    AppManager.user.scoreModel = responseObject[@"data"];
                    [self setUserData];
                    return;
                }
            }];
        }
        [self setUserData];
    }];
}

- (IBAction)toMeInfo:(id)sender {
    if ([AppManager userIsLogin]) {
        [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"MeInfoController" bundle:nil] instantiateInitialViewController] animated:true];
        return;
    }
    [self presentViewController:[[FountManager share] loginViewControl] animated:true completion:^{}];
}

- (IBAction)toMessage:(UIButton *)sender {
    MessageController *controller = [[MessageController alloc] init];
    [self.navigationController pushViewController:controller animated:true];
}

- (IBAction)toOrderAction {
    MeOrderController *orderControl = [[MeOrderController alloc] init];
    [self.navigationController pushViewController:orderControl animated:true];
}

- (IBAction)toMyCollectViewControl:(UIButton *)sender {
    MeCollectViewController *viewControl = [[MeCollectViewController alloc] init];
    [self.navigationController pushViewController:viewControl animated:true];
}

- (IBAction)psdSettingACtion:(UIButton *)sender {
    ChangePsdViewController *changePsdViewCon = [[ChangePsdViewController alloc] initWithNibName:@"ChangePsdViewController" bundle:nil];
    [self.navigationController pushViewController:changePsdViewCon animated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
